package com.zzzy.doc.controller.mydoc;


import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.doc.service.mydoc.MyDocSignService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@Tag(name = "我的公文-签章", description = "doc115")
@Validated
@RestController
@RequestMapping("/doc/my")
public class MyDocSignController {

    @Resource
    private MyDocSignService myDocSignService;

    @PutMapping("/applySign")
    @Operation(summary = "申请签章")
    @Parameter(name = "id", description = "公文ID", required = true)
    @Parameter(name = "userId", description = "用户ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:my:update')")
    public AjaxResult applySign(@NotNull(message = "公文ID不能为空") Long id, @NotNull(message = "用户ID不能为空") Long userId) {
        myDocSignService.applySign(id, userId);
        return AjaxResult.success();
    }
    
    @PostMapping(value = "/updateSignFile/{id}")
    @Operation(summary = "保存签章中文件")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:update')")
    public void updateSignFile(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id , HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        byte[] byteArray = FileUtil.toByteArray(fs.getFileStream());
        myDocSignService.updateFinalPdf(id, byteArray);
        fs.setCustomSaveResult("ok");
        fs.close();
    }

    @PutMapping("/signComplete/{id}")
    @Operation(summary = "签章完毕进入分发")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:my:update')")
    public AjaxResult signComplete(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        myDocSignService.signComplete(id);
        return AjaxResult.success();
    }

}
